package view;

import java.awt.Component;
import java.awt.Container;
import java.awt.Dimension;
import java.awt.LayoutManager;
import java.awt.Window;
import java.lang.reflect.Method;

import javax.swing.JFrame;
import javax.swing.JPanel;

public class MainFrame extends JFrame {
	public GamePanel gamePanel;
	public SidePanel infoPanel;
	public static MainFrame MAIN_FRAME;

	public MainFrame() {
		super("Achtung, ...");

		enableOSXFullscreen(this);
		init();
		MAIN_FRAME = this;
	}

	@SuppressWarnings({ "unchecked", "rawtypes" })
	public static void enableOSXFullscreen(Window window) {
		try {
			Class util = Class.forName("com.apple.eawt.FullScreenUtilities");
			Class params[] = new Class[] { Window.class, Boolean.TYPE };
			Method method = util.getMethod("setWindowCanFullScreen", params);
			method.invoke(util, window, true);
		} catch (ClassNotFoundException e1) {
		} catch (Exception e) {
		}
	}

	public void init() {
		JPanel mainPanel = (JPanel) getContentPane();
		mainPanel.setLayout(new LayoutManager() {
			@Override
			public void removeLayoutComponent(Component arg0) {
			}
			
			@Override
			public Dimension preferredLayoutSize(Container arg0) {
				return gamePanel.getPreferredSize();
			}
			
			@Override
			public Dimension minimumLayoutSize(Container arg0) {
				return gamePanel.getMinimumSize();
			}
			
			@Override
			public void layoutContainer(Container c) {
				gamePanel.setBounds(0, 0, c.getWidth(), c.getHeight());
				Dimension dimension = infoPanel.getPreferredSize();
				infoPanel.setBounds(0, 0, dimension.width, c.getHeight());
			}
			
			@Override
			public void addLayoutComponent(String arg0, Component arg1) {
			}
		});

		infoPanel = new SidePanel();
		gamePanel = new GamePanel();

		mainPanel.add(infoPanel);
		mainPanel.add(gamePanel);

		setDefaultCloseOperation(EXIT_ON_CLOSE);
	}

	public GamePanel getGamePanel() {
		return gamePanel;
	}

	public SidePanel getInfoPanel() {
		return infoPanel;
	}
}
